package com.lideru.backstage.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lideru.backstage.entity.Book;
import com.lideru.backstage.entity.BrowserHistory;
import com.lideru.backstage.mapper.BookMapper;
import com.lideru.backstage.mapper.BrowserHistoryMapper;
import com.lideru.backstage.utils.JWTUtils;
import com.lideru.backstage.utils.common.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * @author lideru
 */
@RestController
@RequestMapping("/browserHistory")
public class BrowserHistoryController {
    @Resource
    BrowserHistoryMapper browserHistoryMapper;
    @Resource
    private BookMapper bookMapper;

    @PostMapping
    public Result<?> save(@RequestBody BrowserHistory bH, HttpServletRequest request) {
        BrowserHistory b = browserHistoryMapper.selectOne(Wrappers.<BrowserHistory>lambdaQuery().eq(BrowserHistory::getBookId, bH.getBookId()).
                eq(BrowserHistory::getUserId, JWTUtils.getUserId(request.getHeader("token"))));
        if (b != null) {
            Book book = bookMapper.selectById(b.getBookId());
            if (book != null) {

                b.setName(book.getName());
                b.setCover(book.getCover());
                b.setGmtModified(new Date());
                b.setUserId(JWTUtils.getUserId(request.getHeader("token"))
                );
                browserHistoryMapper.updateById(b);
                return Result.success();
            } else {
                return Result.error("-1", "新增历史记录失败！");
            }
        } else {
            Book book = bookMapper.selectById(bH.getBookId());
            if (book != null) {
                BrowserHistory browserHistory = new BrowserHistory();
                browserHistory.setCover(book.getCover());
                browserHistory.setName(book.getName());
                browserHistory.setBookId(book.getId());
                browserHistory.setUserId(JWTUtils.getUserId(request.getHeader("token")));
                browserHistory.setGmtCreate(new Date());
                browserHistory.setGmtModified(new Date());
                int i = browserHistoryMapper.insert(browserHistory);
                if (i < 1) {
                    return Result.error("-1", "新增历史记录失败！");
                }
                return Result.success();
            } else {
                return Result.error("-1", "新增历史记录失败！");
            }

        }


    }

    @PutMapping
    public Result<?> update(@RequestBody BrowserHistory browserHistory) {
        browserHistory.setGmtModified(new Date());
        int i = browserHistoryMapper.updateById(browserHistory);
        if (i < 1) {
            return Result.error("-1", "修改书本类型失败！");
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        BrowserHistory browserHistory = browserHistoryMapper.selectOne(Wrappers.<BrowserHistory>lambdaQuery()
                .eq(BrowserHistory::getId, id));
        if (browserHistory != null) {
            browserHistoryMapper.deleteById(id);
            return Result.success();
        }
        return Result.error("403", "你没有权限");

    }

    @GetMapping("getAllBrowserHistory")
    public Result<?> getAllBrowserHistory() {
        return Result.success(browserHistoryMapper.selectList(null));
    }

    @GetMapping("getUserAllBrowserHistory")
    public Result<?> getUserAllBrowserHistory(@RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "10") Integer pageSize,
                                              @RequestParam(defaultValue = "") String search, HttpServletRequest request) {
        return Result.success(
                browserHistoryMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.<BrowserHistory>lambdaQuery().
                        eq(BrowserHistory::getUserId, JWTUtils.getUserId(request.getHeader("token")))
                        .last("order by gmt_modified  desc")));
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search, HttpServletRequest request) {
        return Result.success(
                browserHistoryMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.<BrowserHistory>lambdaQuery().
                        eq(BrowserHistory::getUserId, JWTUtils.getUserId(request.getHeader("token")))
                        .like(BrowserHistory::getName, search)));
    }


    /**
     * 统计数据
     *
     * @return
     */
    @GetMapping("/count")
    public Result<?> count(HttpServletRequest request) {
//        User user = getUser(); // 当前登录的用户信息
        return Result.success(browserHistoryMapper.countBrowserHistory( JWTUtils.getUserId(request.getHeader("token"))));
    }



}
